﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SilverlightPlayer
{
    public partial class PlaylistItem : UserControl
    {
        public PlaylistItem()
        {
            InitializeComponent();
            VisualStateManager.GoToState(this, "NotPlaying", false);
        }

        Track _track;
        public Track Track
        {
            get
            {
                return _track;
            }
            set
            {
                _track = value;
                imgContent.Source = new BitmapImage(new Uri(_track.Thumbnail, UriKind.RelativeOrAbsolute));
                imgContentReflection.Source = imgContent.Source;
            }
        }

        bool _isPlaying;
        public bool IsPlaying
        {
            get
            {
                return _isPlaying;
            }
            set
            {
                _isPlaying = value;
                if (IsPlaying) VisualStateManager.GoToState(this, "Playing", true);else VisualStateManager.GoToState(this, "NotPlaying", true);
            }
        }

        bool mouseIsOver;

        private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseOver", true);
            mouseIsOver = true;
        }

        private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Normal", true);
            mouseIsOver = false;
        }

        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(this, "Pressed", true);
            IsPlaying = true;
            if (IsSelected != null) IsSelected(this, new TrackIsSelectedEventArgs(Track));

        }

        private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (mouseIsOver)
            {
                VisualStateManager.GoToState(this, "MouseOver", true);
            }
            else
            {
                VisualStateManager.GoToState(this, "Normal", true);
            }
        }

        private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsHovered != null) IsHovered(this,new TrackIsSelectedEventArgs(Track));

        }

        public event EventHandler<TrackIsSelectedEventArgs> IsHovered;
        public event EventHandler<TrackIsSelectedEventArgs> IsSelected;


    }
}
